// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  if (event.content.length >= 20) {
    content = event.content.substr(0, 19) + '…'
  }
  if (event.originalContent.length >= 20) {
    originalContent = event.originalContent.substr(0, 19) + '…'
  }
  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: event.openid,
      page: 'pages/discuss/discuss?id=' + event.id,
      lang: 'zh_CN',
      data: {
        thing6: {
          value: originalContent
        },
        thing5: {
          value: content
        },
        thing1: {
          value: event.nickName
        },
        time2: {
          value: event.time
        },
        thing4: {
          value: '有人回复了你的评论，去看看吧~'
        }
      },
      templateId: 'GSNm6M5LHxPYrlXm5k2cCb2Md-yI9zYAQ0n-BRKdPP0',
      miniprogramState: 'formal'
    })
    return result
  } catch (err) {
    return err
  }
}